//
//  CustomAnnotationView.m
//  SHShopping
//
//  Created by 坤鼎 on 2019/9/17.
//  Copyright © 2019 kunding. All rights reserved.
//

#import "CustomAnnotationView.h"

@interface CustomAnnotationView ()


@end

@implementation CustomAnnotationView

#define kCalloutWidth       107
#define kCalloutHeight      32

- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
//    阴影层view
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
    backgroundView.layer.shadowColor   = [UIColor colorWithWhite:0 alpha:0.18].CGColor;
    backgroundView.layer.shadowOffset  = CGSizeMake(0, 3);
    backgroundView.layer.shadowOpacity = 1;
    backgroundView.layer.shadowRadius  = 5;
//    渐变色层view
    UIView *colorView = [[UIView alloc] initWithFrame:backgroundView.bounds];
    [colorView gradientViewColorArray:@[(id)[UIColorFromRGB(0xFFFFFF) CGColor], (id)[UIColorFromRGB(0xE7EBF8) CGColor]] pointArr:@[@(0.5), @(0), @(0.5), @(1)]];
//    圆角层view
    self.titleLabel = [[UILabel alloc] initWithFrame:backgroundView.bounds];
    self.titleLabel.layer.cornerRadius = 16;
    self.titleLabel.layer.masksToBounds = YES;
    self.titleLabel.font = FONT_BOLD(13);
    self.titleLabel.textColor = [UIColor blackColor];
    self.titleLabel.text = @"title";
    self.titleLabel.textAlignment = YES;
    
    [backgroundView addSubview:colorView];
    [backgroundView addSubview:self.titleLabel];
    [self addSubview:backgroundView];
    {//大头针
        UIImageView *imageView = [[UIImageView alloc] initWithImage:ImageNamed(@"大头针")];
        imageView.frame = CGRectMake((102 - 30) / 2, GetViewYH(self.titleLabel) + 5, 30, 42);
        [self addSubview:imageView];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    if (self.selected == selected) {
        return;
    }
    UIColor *color = [self toUIColorByStr:self.titleLabelColor];
    if (selected) {
        self.titleLabel.layer.borderColor = color.CGColor;
        self.titleLabel.layer.borderWidth = 1;
        self.titleLabel.textColor = color;
    } else {
        self.titleLabel.layer.borderColor = [UIColor clearColor].CGColor;
        self.titleLabel.layer.borderWidth = 0;
        self.titleLabel.textColor = [UIColor blackColor];
    }
    [super setSelected:selected animated:animated];
}
// 将16进制字符串转换成 UIColor
- (UIColor *)toUIColorByStr:(NSString *)colorStr {
    NSString *cString = [[colorStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
    if ([cString length] != 6) return [UIColor blackColor];
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:1.0f];
}

@end
